import java.util.Deque;
import java.util.LinkedList;

public class Sort {

    /**
     * 插入排序
     * 时间复杂度：O(N^2）
     *          最好情况复杂度O(N)
     *                当时间趋于有序的时候排序速度非常快
     *                         一般场景是数据基本有序，建议使用插入排序
     * 空间复杂度：O(1)
     * 稳定性：稳定
     *        如果一个排序是稳定的，那么就可以实现为不稳定的
     *        但如果一个排序本身是不稳定的，你没有办法实现稳定的
     * @param array
     */

    public  static void insertSort(int []   array){
        for (int i = 1; i <array.length ; i++) {
            int tmp  = array[i];
            int j = i - 1;
            for (; j >= 0 ; j --) {
                if (array[j] > tmp){
                    array[j + 1] = array[j];
                }else {
                    break;
                }
            }
            array[j +1 ] = tmp;
        }
    }

    /**
     * 希尔排序
     * 时间复杂度:O(N^1.3)-O(N^1.5)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     * @param array
     */
    public static void shellSort(int[] array){
        int gap = array.length;
        while (gap > 1){
            gap /= 2;
            shell(array,gap);
        }
    }
    public static void shell(int[] array,int gap){
        for (int i = gap; i <array.length ; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp ){
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    /**
     * 选择排序
     * 时间复杂度：O(N^2)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     * @param array
     */
    public static void selectSort(int[] array){
        for (int i = 0; i <array.length ; i++) {
            int minIndex = i;
            int j = i+1;
            for (; j < array.length; j++) {
                if (array[j] < array[minIndex]){
                    minIndex = j;
                }
            }
        swap(array,i,minIndex);
        }
    }
    private static void swap(int[] array,int i,int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;

    }
    public static void selectSort2(int[] array){
        int left = 0;
        int righ = array.length-1;
        while (left < righ){
            int minIndex = left;
            int maxIndex = left;

            for (int i = left+1; i <= righ; i++) {
                if(array[i] < array[minIndex]){
                    minIndex = i ;
                }

                if(array[i] > array[maxIndex]){
                    maxIndex= i ;
                }

            }

            swap(array,minIndex,left);
            if (maxIndex == left) {
                maxIndex = minIndex;
            }
            swap(array, maxIndex, righ);
            left++;
            righ--;
        }
    }

    /**
     * 堆排序：
     * 时间复杂度：O(N*logn)
     * 空间复杂度:O(1)
     * 稳定性：不稳定
     *
     * @param array
     */
    public static void heapSort(int[] array){
        createBigHeap(array);
        int end = array.length-1;
        while (end > 0 ){
            swap(array,0,end);
            shiftDown(array,0,end);
            end--;
        }
    }

    private static void createBigHeap(int[] array){
        for (int parent =(array.length-1-1)/2 ; parent >= 0  ; parent--) {
            shiftDown(array,parent,array.length);
        }
    }
    private static void shiftDown(int[] array,int parent ,int len){
        int  child= 2*parent+1;
        while (child < len) {
            if (child+1 < len && array[child] <array[child+1]){
                child++;
            }
            if (array[child] > array[parent]){
                swap(array,child,parent);
                parent = child;
                child = 2*parent+1;
            }else {
                break;
            }
        }

    }

    /**
     * 冒泡排序
     * 时间复杂度：O(N^2)
     * 空间复杂度：O(1)
     * 稳定性：稳定的
     * @param array
     */

    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j] > array[j+1]){
                    swap(array,j,j+1);
                    flg = true;
                }
            }
            if (flg == false){
                return;
            }
        }

    }

    /**
     * 快速排序
     *
     * @param array
     */
    public static void quickSort(int[] array){
        quick(array,0,array.length-1);

    }
    private  static void quick(int[] array,int start ,int end){
        if (start >= end ){
            return;
        }

        if (end - start +1 <= 14){
            //插入排序
            insertSort2(array,start,end);
            return;
        }

        //System.out.println("start:"+start+"end:"+end);

        int index=midTbree(array,start,end);
        swap(array,index,start);

        int pivot = partition(array,start,end);

        quick(array,start,pivot-1);
        quick(array,pivot+1,end);

    }


    private  static void insertSort2(int []  array,int left ,int right){
        for (int i = left+1; i <=right ; i++) {
            int tmp  = array[i];
            int j = i - 1;
            for (; j >= left; j --) {
                if (array[j] > tmp){
                    array[j + 1] = array[j];
                }else {
                    break;
                }
            }
            array[j +1 ] = tmp;
        }
    }

//优化
    private static int midTbree(int[] array,int left ,int right){
        int mid = (left + right)/2;
        if (array[left] < array[right]){
            if (array[mid] < array[left]){
                return left;
            }else if (array[mid] > array[right]){
                return right;
            }else {
                return mid;
            }
        }else {
            if (array[mid] < array[right]){
                return right;
            }if (array[mid] > array[left]){
                return left;
            }else {
                return mid;
            }
        }
    }
//挖坑法
    private  static  int partition(int[] array ,int left ,int right){
        int tmp = array[left];
        while (left < right){
            while (left < right && array[right] >= tmp){
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp){
                left++;
            }
           array[right] = array[left];

        }
        array[left] = tmp;
        return left;
    }

//Hoare法
    private  static  int partition2(int[] array ,int left ,int right){
        int tmp = array[left];
        int i = left;
        while (left < right){
            while (left < right && array[right] >= tmp){
                right--;
            }
            while (left < right && array[left] <= tmp){
                left++;
            }
           swap(array,left,right);

        }
        swap(array,left,i);
        array[left] = tmp;
        return left;

    }


    /**
     * 非递归实现排序
     * @param array
     */
    public static void quickSort2(int[] array){
        Deque<Integer> stack = new LinkedList<>();
        int left = 0 ;
        int right = array.length - 1;
        int pivot = partition(array,left,right);
        if (pivot > left + 1 ){
            stack.push(left);
            stack.push(pivot-1);
        }
        if(pivot < right-1){
            stack.push(pivot+1);
            stack.push(right);
        }


        while (!stack.isEmpty()){
            right = stack.pop();
            left = stack.pop();
            pivot = partition(array,left,right);
            if (pivot > left + 1 ){
                stack.push(left);
                stack.push(pivot-1);
            }
            if(pivot < right-1){
                stack.push(pivot+1);
                stack.push(right);
            }
        }
    }

    /**
     * 归并排序
     * 时间复杂度：N*LogN
     * 空间复杂度：N
     *稳定性：稳定
     *
     *        插入   冒泡     归并
     * @param array
     */
    public static void mergeSort1(int[] array){
        mergeSortFunc(array,0,array.length-1);


    }
    private static void mergeSortFunc(int[] array ,int left ,int right){
        if(left >= right){
            return;
        }

        int mid = (left+right)/2;

        mergeSortFunc(array,left,mid);
        mergeSortFunc(array,mid+1,right);
        merge(array,left,right,mid);
    }
    private static void merge(int[] array,int start,int end ,int mid){
        int s1 = start;

        int s2 = mid+1;

        int[] tmp = new int[end-start+1];
        int k = 0;//tmp 数组的下标
        while (s1 <= mid &&s2 <= end){
            if (array[s1] <= array[s2]){
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];

            }
        }
        while (s1 <= mid){
            tmp[k++] = array[s1++];
        }
        while (s2 <= end){
            tmp[k++] = array[s2++];
        }

        for (int i = 0; i < tmp.length; i++) {

            array[i+start] = tmp[i];
        }
    }


    public static void mergeSort2(int[] array){

        int gap = 1 ;
        while (gap < array.length){
            //i += gap *2 当前gap组时候，去排序下一组
            for (int i = 0; i <array.length ; i += gap*2) {
                int left = i;
                int mid = left+gap-1;//可能越界
                if (mid >= array.length){
                    mid = array.length-1;
                }
                int right = mid+gap;//可能越界
                if (right >= array.length){
                    right = array.length-1;
                }
                merge(array,left,right,mid);
            }

            //当前为2组有序  下次变4组有序
            gap *= 2;
        }


    }




}


